package team.scau.laboratory_repair.common.beetl;

import lombok.extern.apachecommons.CommonsLog;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.beetl.core.GeneralVarTagBinding;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author 30254
 * creadtedate:2018/8/20
 */
@Service
@Scope("prototype")
@CommonsLog
public class AuthTag extends GeneralVarTagBinding {

    @FunctionalInterface
    private interface RenderMethod{
        String render();
    }
    /**
     * 权限行为集合
     */

    //初始化权限行为

    private RenderMethod renderMethod;

    /**
     * 标签id
     */
    private String id;
    /**
     * 权限类型
     */
    private String type;

    /**
     * 权限标识
     */
    private String perm;

    /**
     * 按钮文本值
     */
    private String text;
    /**
     * 点击事件
     */
    private String onclick;
    /**
     * name 参数
     */
    private String name;
    /**
     * class 参数
     */
    private String extClass;
    /**
     * 文字图标
     */
    private String iconText;
    /**
     * data-id 参数
     */
    private String dataId;
    /**
     * target 参数
     */
    private String target;
    /**
     * lay-filter 参数
     */
    private String layFilter;
    /**
     * value 参数
     */
    private String value;
    /**
     * checked 参数
     */
    private String checked;

    private String getTagType(String name){
        String temp = (String) getAttributeValue(name);
        temp = temp== null ? "" : temp;
        return  temp;
    }

    private void delegate(){
        switch (type){
            case "button": renderMethod = this:: button;break;
            case "a": renderMethod = this:: a;break;
            case "checkbox": renderMethod = this:: checkbox;break;
//            case "field": renderMethod = this:: field;break;
//            case "tableCol": renderMethod = this:: tableCol;break;
            default: renderMethod = this:: empty;break;
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public void render() {
            this.type = getTagType("type");

            delegate();

            this.perm = getTagType("perm");
            this.text = getTagType("text");
            this.id = getTagType("id");
            this.onclick = getTagType("onclick");
            this.name = getTagType("name");
            this.extClass = getTagType("extClass");
            this.iconText = getTagType("iconText");
            this.dataId = getTagType("dataId");
            this.target = getTagType("target");
            this.layFilter = getTagType("layFilter");
            this.value = getTagType("value");
            this.checked = getTagType("checked");
        try {
            //根据权限类型调用相关的方法
            ctx.byteWriter.writeString(renderMethod.render());
        } catch (IOException e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
        }
    }

    public String empty(){
        return StringUtils.EMPTY;
    }

    /**
     *
     * @Title: button
     * @Description: 查看、编辑、删除按钮
     * @param
     * @return String
     * @throws
     */
    public String button(){
        StringBuilder sb = new StringBuilder();

        if(SecurityUtils.getSubject().isPermitted(perm)){
            text = StringUtils.isBlank(text) ? "" : text;
            sb.append("<button type=\"button\" class=\"layui-btn ").append(extClass).append("\" ")
                    .append("id=\"").append(id).append("\" ")
                    .append("name=\"").append(name).append("\" ")
                    .append("data-id=\"").append(dataId).append("\" ");

            if(!"".equals(onclick)){
                sb.append("onclick=\"").append(onclick).append("\" ");
            }

            sb.append(">");
            if(!"".equals(iconText)) {
                sb.append("<i class=\"layui-icon\">").append(iconText).append("</i>");
            }
            sb.append(text).append("</button>");
        }

        return sb.toString();
    }

    /**
     *
     * @Title: a
     * @Description: 链接按钮
     * @param
     * @return String
     * @throws
     */
    public String a(){
        StringBuilder sb = new StringBuilder();

        if(SecurityUtils.getSubject().isPermitted(perm)){
            text = StringUtils.isBlank(text) ? "" : text;
            sb.append("<a type=\"button\" class=\"layui-btn ").append(extClass).append("\" ")
                    .append("id=\"").append(id).append("\" ")
                    .append("name=\"").append(name).append("\" ")
                    .append("data-id=\"").append(dataId).append("\" ");

            if(!"".equals(onclick)){
                sb.append("onclick=\"").append(onclick).append("\" ");
            }
            if(!"".equals(target)){
                sb.append("target=\"").append(target).append("\" ");
            }

            sb.append(">");
            if(!"".equals(iconText)) {
                sb.append("<i class=\"layui-icon\">").append(iconText).append("</i>");
            }
            sb.append(text).append("</a>");
        }

        return sb.toString();
    }

    /**
     *
     * @Title: checkbox
     * @Description: 复选框
     * @param
     * @return String
     * @throws
     */
    public String checkbox(){
        StringBuilder sb = new StringBuilder();

        if(SecurityUtils.getSubject().isPermitted(perm)){
            text = StringUtils.isBlank(text) ? "" : text;
            sb.append("<input type=\"checkbox\"")
                    .append("lay-filter=\"").append(layFilter).append("\" ")
                    .append("id=\"").append(id).append("\" ")
                    .append("name=\"").append(name).append("\" ")
                    .append("title=\"").append(text).append("\" ")
                    .append("value=\"").append(value).append("\" ")
                    .append("data-id=\"").append(dataId).append("\" ");

            sb.append(checked);
            sb.append(">");

        }
        return sb.toString();
    }



    /**
     * @Title: field
     * @Description: 销售统计字段
     * @return
     */
//    public String field() {
//        StringBuilder sb = new StringBuilder();
//        if (SecurityUtils.getSubject().isPermitted(perm)) {
//            text = StringUtils.isBlank(text) ? "" : text;
//            sb.append(text);
//            if (name.equals("总广告费(美金)")) {
//                sb.append("    formatter : function(cellvalue, options, rowdata){\n" +
//                        "        var html = '';\n" +
//                        "        if(rowdata.id > 0){\n" +
//                        "            html = '<input type=\"text\" id=\"totalAdUSD_' + rowdata.id + '\" value=\"' + (cellvalue!=null?cellvalue:'') + '\" class=\"layui-input\" style=\"display: inline-block;width:95%;margin-top: 4px;\">' +\n" +
//                        "                    '<input type=\"hidden\" name=\"id\" value=\"' + rowdata.id + '\" />';\n" +
//                        "        }else {\n" +
//                        "            html = '<input type=\"text\" readonly id=\"totalAdUSD_' + rowdata.id + '\" value=\"' + (cellvalue!=null?cellvalue:'') + '\" class=\"layui-input\" style=\"display: inline-block;width:95%;margin-top: 4px;\">';\n" +
//                        "        }\n" +
//                        "        return html;\n" +
//                        "    }\n" +
//                        "},");
//            }
//            if(name.equals("总营业额(美金)")){
//                sb.append(" formatter : function(cellvalue, options, rowdata){\n" +
//                        "            var html = '';\n" +
//                        "            if(rowdata.id > 0){\n" +
//                        "                html = '<input type=\"text\" id=\"totalTurnoverUSD_' + rowdata.id + '\" value=\"' + (cellvalue!=null?cellvalue:'') + '\" class=\"layui-input\" style=\"display: inline-block;width:95%;margin-top: 4px;\">';\n" +
//                        "            }else {\n" +
//                        "                html = '<input type=\"text\" readonly id=\"totalTurnoverUSD_' + rowdata.id + '\" value=\"' + (cellvalue!=null?cellvalue:'') + '\" class=\"layui-input\" style=\"display: inline-block;width:95%;margin-top: 4px;\">';\n" +
//                        "            }\n" +
//                        "            return html;\n" +
//                        "        }\n" +
//                        "    },");
//            }
//            if(name.equals("额外费用(美金)")){
//                sb.append(" {\n" +
//                        "        name : \"extraCostUSD\",\n" +
//                        "        index : \"extraCostUSD\",\n" +
//                        "        width : 120,\n" +
//                        "        title:false,\n" +
//                        "        editable:true,\n" +
//                        "        align : \"left\",\n" +
//                        "        formatter : function(cellvalue, options, rowdata){\n" +
//                        "            var html = '';\n" +
//                        "            if(rowdata.id > 0){\n" +
//                        "                html = '<input type=\"text\" id=\"extraCostUSD_' + rowdata.id + '\" value=\"' + (cellvalue!=null?cellvalue:'') + '\" class=\"layui-input\" style=\"display: inline-block;width:95%;margin-top: 4px;\">';\n" +
//                        "            }else {\n" +
//                        "                html = '<input type=\"text\" readonly id=\"extraCostUSD_' + rowdata.id + '\" value=\"' + (cellvalue!=null?cellvalue:'') + '\" class=\"layui-input\" style=\"display: inline-block;width:95%;margin-top: 4px;\">';\n" +
//                        "            }\n" +
//                        "            return html;\n" +
//                        "        }\n" +
//                        "    },");
//            }
//            if(name.equalsIgnoreCase("crawler")){
//                sb.append(",{\n" +
//                        "        name : \"enable\",\n" +
//                        "        index : \"enable\",\n" +
//                        "        sortable : true,\n" +
//                        "        width : 100,\n" +
//                        "        align : \"center\",\n" +
//                        "        formatter : function(cellvalue, options, rowdata){\n" +
//                        "            return '<form class=\"layui-form\" style=\"padding-top: 5px\">' +\n" +
//                        "                '<input type=\"checkbox\"lay-filter=\"c_enable\" id=\"\" name=\"enable\" title=\"启用\" value=\"'+(cellvalue?'on':'off')+'\" data-id=\"' + rowdata.productId+ '\" '+(cellvalue?'checked':'')+'>' +\n" +
//                        "                '</form>';\n" +
//                        "        }\n" +
//                        "    }");
//            }
//        }
//        return sb.toString();
//
//    }


    /**
     * 表格列头元素
     * @return
     */
//    public String tableCol(){
//        // "总单量", "总广告费(美金)", "总营业额(美金)", "额外费用(美金)", "总广告费(RMB)",
//        // "总营业额(RMB)", "额外费用(RMB)", "广告占比", "总成本", "实际利润", "总回款"
//
//        //表格列头
//        StringBuilder sb = new StringBuilder();
//        if(SecurityUtils.getSubject().isPermitted(perm)){
//            //权限编码
//            String[] pcodes={"biz:saleReport:listallorder","biz:saleReport:listADUSD",
//                    "biz:saleReport:listtotalTurnoverUSD","biz:saleReport:listextraCostUSD","biz:saleReport:listtotalAdRMB",
//                    "biz:saleReport:listtotalTurnoverRMB","biz:saleReport:listextraCostRMB",
//                    "biz:saleReport:listratioStr","biz:saleReport:listtotalCost",
//                    "biz:saleReport:listactualProfit","biz:saleReport:listtotalProfit","biz:saleReport:listReturns"};
//            //字段权限名
//            String[] pnames={"\"总单量\"", "\"总广告费(美金)\"", "\"总营业额(美金)\"", "" +
//                    "\"额外费用(美金)\"", "\"总广告费(RMB)\"", "\"总营业额(RMB)\"", "\"额外费用(RMB)\"", "\"广告占比\"", "\"总成本\"", "\"实际利润\"", "\"总回款\"","\"退货量\""};
//            for(int i=0;i<pcodes.length;i++){
//                if(SecurityUtils.getSubject().isPermitted(pcodes[i])||SecurityUtils.getSubject().hasRole("super pc")){
//                    sb.append(",").append(pnames[i]).append("");
//                }
//
//            }
//        }
//        return sb.toString();
//    }
}